package com.kingyea.mobilepolice.zuul.utils;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

/**
 * @description：server端接口 统一返回数据格式
 * @author: Mr.Lin
 * @date: created in 11:18 2017/11/9
 * @version: v_1.0.0
 */
public class Result<T> implements Serializable {

    /**
     * 错误码.
     */
    private String code;

    /**
     * 提示信息.
     */
    private String msg;

    /**
     * 具体的内容.
     */
    private T data;

    public Result() {
    }

    public Result(String code) {
        this.code = code;
        this.msg = StateCode.getMsg(code);
    }

    public Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result getSuccessResult(Object data) {
        Result r = new Result();
        r.setDefaultStatus();
        r.setData(data);
        return r;
    }

    public static Result getFailedResult(Object data) {
        Result r = new Result();
        r.setSystemErrorStatus();
        r.setData(data);
        return r;
    }

    public static void main(String[] args) {
        System.out.println(Result.getSuccessResult("okokok"));
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    //设置默认状态（操作成功）
    public void setDefaultStatus() {
        this.code = StateCode.OPERATION_SUCCESS;
        this.msg = StateCode.getMsg(StateCode.OPERATION_SUCCESS);
    }

    //设置系统错误状态
    public void setSystemErrorStatus() {
        this.code = StateCode.SYSTEM_ERROR;
        this.msg = StateCode.getMsg(StateCode.SYSTEM_ERROR);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
